package com.dhcc.bpm.business.papc.controller;

import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.mp.support.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.bpm.common.enums.ConfigType;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import com.dhcc.bpm.common.utils.DateUtils;
import com.dhcc.bpm.common.utils.UUIDGenerator;
import com.dhcc.bpm.modules.orga.entity.App;
import com.dhcc.bpm.modules.orga.service.IAppService;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

/**
 * @ClassName: RenovateAppController
 * @Description: 渠道启停
 * @author djj
 * @date 2020-05-28 10:27:15
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-papc/renovate")
public class RenovateAppController  {
	private final static Logger log = LoggerFactory.getLogger(RenovateAppController.class);


	private IAppService appService;
	@Autowired
	private RedisMessageSender redisMessageSender;

	/**
	 * 刷新机构渠道信息
	 * 
	 * @param json
	 * @return
	 */
	@PostMapping("/reva")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "修改", notes = "传入JSONObject")
	public JSONObject update(@Valid @RequestBody JSONObject json) {
		// 请求号
		String requestNo = json.getString("requestNo");
		// 请求数据
		JSONObject requestData = json.getJSONObject("requestData");
		// 业务类型
		String businessType = requestData.getString("BussinesType");
		// 业务编号
		String bussinesCode = requestData.getString("BussinesCode");
		// 操作动作
		String openStat = requestData.getString("OpenStat");
		Boolean boo = null;

		// 组装返回报文
		JSONObject response = new JSONObject();
		// 响应流水号
		response.put("responseNo", requestNo);
		// 响应时间
		response.put("responseTime", DateUtils.dateTimeNow());
		// 流水号
		response.put("PvdrSrlNo", UUIDGenerator.getUUID());
		if ("01".equals(businessType)) {
			// 业务类型为渠道
			App app = new App();
			app.setAppId(bussinesCode);
			App ap = appService.getOne(Condition.getQueryWrapper(app));
			if ("01".equals(openStat)) {
				// 01为打开
				ap.setAppStatus("1");
			} else if ("02".equals(openStat)) {
				// 02为关闭
				ap.setAppStatus("0");
			} else {
				// 返回信息
				response.put("RetMsg", "操作动作有误");
				// 返回编码
				response.put("RetCode", "0001");
				return response;
			}
			boo = appService.updateById(ap);
		} else {
			// 返回信息
			response.put("RetMsg", "业务类型有误");
			// 返回编码
			response.put("RetCode", "0001");
			return response;
		}
		try {
			String message = redisMessageSender.format(ConfigType.APIWHITEBLACK.name(), "刷新API黑白名单");
			redisMessageSender.sendMessageToGateway("", message);
		} catch (Exception e) {
			log.error("刷新API黑白名单失败:{}", e);
			// 返回信息
			response.put("RetMsg", "交易失败");
			// 返回编码
			response.put("RetCode", "0001");
			return response;
		}
		if (boo) {
			// 返回信息
			response.put("RetMsg", "交易成功");
			// 返回编码
			response.put("RetCode", "0000");
			//
			response.put("responseData", "");

		} else {
			// 返回信息
			response.put("RetMsg", "交易失败");
			// 返回编码
			response.put("RetCode", "0001");
			//
			response.put("responseData", "");
		}
		return response;
	}

}
